package org.jeecg.modules.fk.service;

import org.jeecg.modules.fk.vo.FxtjVO;
import org.jeecg.modules.fk.vo.FxydVO;
import org.jeecg.modules.fk.vo.RiskResultVO;
import org.jeecg.modules.fk.vo.indicator.IndicatorDefineVO;
import org.jeecg.modules.fk.vo.indicator.IndicatorStatisticVO;
import org.jeecg.modules.fk.constant.ZbTypeEnum;
import org.jeecg.modules.fk.repository.FkDaoRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 企业所得税 相关
 */
@Slf4j
@Service
public class RiskZbService {
    @Autowired
    private FkDaoRepository daoRepository;

    @Autowired
    private RiskIndicatorService riskIndicatorService;
    @Autowired
    private RiskResultService riskResultService;

    /**
     * 指标风险
     *
     * @param zbType
     */
    public List<IndicatorStatisticVO> zbFx(ZbTypeEnum zbType) {
        return this.riskIndicatorService.getDefineGroup(zbType);
    }

    /**
     * 根据纳税人识别号 查询 指标风险
     *
     * @param nsrsbh 纳税人识别号
     * @param skssqq 税款所属期起
     * @param skssqz 税款所属期止
     */
    public List<FxtjVO> zbFxByNsrsbh(ZbTypeEnum zbType, String nsrsbh, String skssqq, String skssqz) {
        //FkTypeEnum fkType = FkTypeEnum.QYSDS;
        //获取风险结果
        Map<String, RiskResultVO> riskResultMap = this.riskResultService.getriskResultMap(ZbTypeEnum.QYSDS, nsrsbh, skssqq, skssqz);

        List<IndicatorStatisticVO> defineGroupList = this.riskIndicatorService.getDefineGroup(zbType);

        //组装结果
        return defineGroupList.stream()
                .map(e -> {
                    FxtjVO fxtjVO = new FxtjVO();
                    BeanUtils.copyProperties(e, fxtjVO);

                    List<IndicatorDefineVO> defineList = e.getDefineList();
                    fxtjVO.setYwxlSl(defineList.size());
                    
                    List<FxydVO> fxydList = defineList.stream()
                            .filter(k -> { //过滤没有疑点的数据
                                RiskResultVO riskResultVO = riskResultMap.get(k.getZbbh());
                                return riskResultVO != null;
                            })
                            .map(k -> {
                                //指标结果
                                RiskResultVO riskResultVO = riskResultMap.get(k.getZbbh());

                                FxydVO fxydVO = new FxydVO();
                                BeanUtils.copyProperties(k, fxydVO);
                                fxydVO.setZbResult(riskResultVO.getZbResult());
                                return fxydVO;
                            })
                            .collect(Collectors.toList());

                    fxtjVO.setYdSl(fxydList.size());
                    fxtjVO.setItems(fxydList);
                    return fxtjVO;
                })
                .collect(Collectors.toList());


    }
}
